import { CONDITION_KEYS } from '@shared';

const includePattern = createConditionPattern(CONDITION_KEYS.INCLUDE);
const excludePattern = createConditionPattern(CONDITION_KEYS.EXCLUDE);
const endPattern = new RegExp(`\\/\\/(?:\\s+)?${CONDITION_KEYS.END}`, 'g');

const endLabel = `,{"${CONDITION_KEYS.END}": true},`;
// 匹配条件编译的正则表达式
function createConditionPattern(condition) {
  const pattern = `\\/\\/(?:\\s+)?${condition}([A-Z\\d\\-\\|\\s]+)`;
  return new RegExp(pattern, 'g');
}
// 将条件编译内容替换为 JSON 格式的插槽
export function replaceConditionByLabel(data) {
  return data
    .replace(includePattern, `,{"${CONDITION_KEYS.INCLUDE}": "$1"},`)
    .replace(excludePattern, `,{"${CONDITION_KEYS.EXCLUDE}": "$1"},`)
    .replace(endPattern, endLabel);
}
// 移除产生的额外的符号
export function removeExcessSign(data) {
  return data.replace(/,{2}/g, ',').replace(/,\]/g, ']');
}
